home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 374 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  5.4 KB

  1. Path: mail2news.demon.co.uk!wbriscoe.demon.co.uk
  2. From: walter briscoe <walter@wbriscoe.demon.co.uk>
  3. Newsgroups: comp.std.c
  4. Subject: Re: setjmp usage question
  5. Date: Sun, 25 Feb 96 12:16:10 GMT
  6. Message-ID: <825250570snz@wbriscoe.demon.co.uk>
  7. References: <4gnusq$7be@senator-bedfellow.MIT.EDU>
  8. Reply-To: walter@wbriscoe.demon.co.uk
  9. X-NNTP-Posting-Host: wbriscoe.demon.co.uk
  10. X-Newsreader: Demon Internet Simple News v1.30
  11. X-Mail2News-Path: wbriscoe.demon.co.uk
  12.  
  13. In article <4gnusq$7be@senator-bedfellow.MIT.EDU>
  14.            tada@athena.mit.edu "Michael J Zehr" writes:
  15.  
  16. > I'm trying to determine whether a particular usage of setjmp is
  17. > sanctioned by the standard and I'm finding a compiler bug or whether my
  18. > usage is non-conforming.  My code boils down to this:
  19.  
  20. [code assigns the result of setjmp to a variable]
  21.  
  22. > Should this code work correctly?  In particular, can a compiler
  23. > increment env_index after longjmp is called?
  24. > [Additional question because I'm curious:  what happens to
  25. > post-increments that are in parameters of the longjmp call?  Are they
  26. > ever executed?  I suppose the general question is where are the sequence
  27. > points when you're using setjmp and lonjmp?]
  28.  
  29. I made a similar mistake some time ago when using an implementation
  30. which did not document the restrictions of setjmp/longjmp and which
  31. behaved inconsistently when run with/without a debugger.  setjmp and
  32. longjmp should be used very carefully when no reasonable alternative
  33. exists.  They seem to be really difficult for many implementors.  Signal
  34. handling is the one thing that is even more difficult and for which
  35. portable usage is very difficult indeed.  Anyway, enough dire warnings.
  36. ANSI/ISO 9899-1990 "7.6 Nonlocal jumps <setjmp.h>" contains:
  37.  
  38. > It is unspecified whether setjmp is a macro or an identifier declared
  39. > with external linkage.  If a macro definition is suppressed in order to
  40. > access an actual function, or a program defines an external identifier
  41. > with the name setjmp, the behavior is undefined.
  42.  
  43. So, you are ill-advised to do "#undef setjmp" or "(setjmp)(env)".
  44.  
  45. > Environmental constraint
  46. >
  47. > An invocation of the setjmp macro shall appear only in one of the
  48. > following contexts:
  49. >
  50. > - the entire controlling expression of a selection or iteration
  51. > statement;
  52. >
  53. > - one operand of a relational or equality operator with the other
  54. > operand an integral constant expression, with the resulting expression
  55. > being the entire controlling expression of a selection or iteration
  56. > statement;
  57. >
  58. > - the operand of a unary !  operator with the resulting expression being
  59. > the entire controlling expression of a selection or iteration statement;
  60. > or
  61. >
  62. > - the entire expression of an expression statement (possibly cast to
  63. > void).
  64.  
  65. This highly restrictive list does not include assigning the result!
  66.  
  67. [snip]
  68.  
  69. > The longjmp function restores the environment saved by the most recent
  70. > invocation of the setjmp macro in the same invocation of the program,
  71. > with the corresponding jmp_buf argument.  If there has been no such
  72. > invocation, or if the function containing the invocation of the setjmp
  73. > macro has terminated execution in the interim, the behavior is undefined.
  74. >
  75. > All accessible objects have values as of the time longjmp was called,
  76. > except that the values of objects of automatic storage duration that are
  77. > local to the function containing the invocation of the corresponding
  78. > setjmp macro that do not have volatile-qualified type and have been
  79. > changed between the setjmp invocation and longjmp call are indeterminate.
  80. >
  81. > As it bypasses the usual function call and return mechanisms, the
  82. > longjmp function shall execute correctly in contexts of interrupts,
  83. > signals and any of their associated functions.  However, if the longjmp
  84. > function is invoked from a nested signal handler (that is, from a
  85. > function invoked as a result of a signal raised during the handling of
  86. > another signal), the behavior is undefined.
  87.  
  88. I had problems with my last usage of setjmp/longjmp in a lex analyser.
  89. My first reaction was to eliminate the technique.  Unfortunately, there
  90. was a failure in re-initialisation code I had written but the point
  91. remains: this is a powerful, dangerous technique.
  92.  
  93. A cruel and unusual test of an implementation would be to do a longjmp
  94. or raise a signal from a callback routine passed to bsearch or qsort.
  95. With luck, following calls of bsearch or qsort would break!  If I had an
  96. implementor's hat on, I would argue that that is undefined behavior
  97. because the callback routine is mandated to return.  As a user, I would
  98. argue that the consequence of a failure to comply with "shall" is not
  99. specified in the standard and that, while failure of the running
  100. function is required by the user action, the consequential failure was
  101. unreasonable.  Comments please from the language lawyers.
  102.  
  103. IMHO, bsearch and qsort are functions which have received insufficient
  104. publicity.  Searching and sorting algorithms are difficult to test and,
  105. for most applications, timing is not critical.  For dealing with data
  106. which can be modelled as an array whose logical size is known at run
  107. time, they form a useful technique. If the key of the array starts at
  108. the beginning of each entry and consists of a fixed number of
  109. characters, the comparison functions memcmp, strcmp, and strncmp even
  110. allow one to call the searc functions without having to write any
  111. auxiliary code.
  112.  
  113. I apologise to the group for digression from the original topic and
  114. express thanks to Michael J Zehr for stimulating the thought.
  115. --
  116. walter briscoe
  117.